--[[/**
-- * 
 * 每累积损失生命[0]% 放技能所需能量 降低[1] , 最多可降低 [2]
 * [0] 损失生命比
 * [1] 放技能所需能量 降低[1]
 * [2] 最多可降低 [2]
-- *
-- */]]

---@class BuffEffect485100 : BaseBuffEffect
BuffEffect485100 = ClientFight.CreateClass("BuffEffect485100", ClientFight.BaseBuffEffect)
local t = BuffEffect485100
t.BUFF_EFFECT_485100_COUNT_COST_HP = "BUFF_EFFECT_485100_COUNT_COST_HP_";
---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local parameters = {...};
    local target = buff.target;
    if (table.getn(parameters) < 3)then
    return IBuffEffect.FAIL;
    end

    local result = parameters[3];
    if (not result:isDamage())then
    return IBuffEffect.FAIL;
    end
    if (not AttackUtils.isDamage(result.state))then
    return IBuffEffect.FAIL;
    end
    local buffEffectValue = buff.buffBean.f_BuffEffectValue;
    local oldVlaue = buff.attributes.attributes[FighterAttributeEnum.ENERGY_COST.id] or 0;
    if (oldVlaue >= buffEffectValue[3])then
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff"
    .. buff.modelId .. " 触发累积损失生命降低sp消耗失败, 已减免值达到上限: " .. oldVlaue);
    return IBuffEffect.FAIL;
    end
    local maxHp = FighterAttributeManager.getFightAttr(target, FighterAttributeEnum.HP);
    local damage = result.damage;
    local currPer =  (damage / maxHp * Global.TEN_THOUSANDTH);

    --已累积损失生命
    local costHpPer = buff.effectParams[t.BUFF_EFFECT_485100_COUNT_COST_HP] or 0;
    costHpPer =costHpPer + currPer;
    buff.effectParams[t.BUFF_EFFECT_485100_COUNT_COST_HP] = costHpPer;--累计减血%% 存储下来

    local step = costHpPer / buffEffectValue[1];
    local totalValue = step * buffEffectValue[2]; --计算应该减免多少 sp消耗
    if (totalValue <= oldVlaue) then
    return IBuffEffect.FAIL;
    end
    if (totalValue >= buffEffectValue[3])then
    totalValue = buffEffectValue[3];
    end
    buff.attributes.attributes[FighterAttributeEnum.ENERGY_COST.id] = totalValue; --减免多少 sp消耗
    local logCostHpPer = costHpPer;
    SkillRoleMgr.savePathFile( "[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff"
    .. buff.modelId .. " 触发累积损失生命降低sp消耗成功, 当前总减免值: " .. oldVlaue .." 本次损失生命:"..currPer .."%% 总累计损失生命:"..logCostHpPer .."%%");
     ClientFight.BaseBuffEffect.action(self, fight, buff, ...);
    return IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE;
end

t.New()